﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace QLHPHP
{
    public partial class FormDangNhap : DevComponents.DotNetBar.Metro.MetroForm
    {
        private SqlConnection con;
        private DataTable dt = new DataTable("TAIKHOAN");
        private SqlDataAdapter da = new SqlDataAdapter();

        private void Connect()
        {
            String cn = Config.ConnectionString;
            try
            {
                con = new SqlConnection(cn);
                con.Open();
            }
            catch (Exception ex)
            {
                ToastNotification.Show(this, "Không thể kết nối cơ sở dữ liệu!");
            }
        }

        private void Disconnect()
        {
            con.Close();
            con.Dispose();
            con = null;
        }
        public FormDangNhap()
        {
            InitializeComponent();
        }

        private void buttonDangNhap_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = System.Data.CommandType.Text;
            command.CommandText = @"Select * From TAIKHOAN
                                            Where (TenTaiKhoan = @TenTaiKhoan)
                                            And (MatKhau = @MatKhau)";
            command.Parameters.Add("@TenTaiKhoan", SqlDbType.NVarChar, 50).Value = txtTenDangNhap.Text;
            command.Parameters.Add("@MatKhau", SqlDbType.NVarChar, 50).Value = txtMatKhau.Text;
            da.SelectCommand = command;
            da.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                MainForm _frmChinh = new MainForm();
                _frmChinh.Show();
                Hide();
                //Disconnect();
            }
            else
            {
                MessageBoxEx.Show("Đăng nhập thất bại!", "ĐĂNG NHẬP", MessageBoxButtons.OK);
            }
            
        }

        private void FormDangNhap_Load(object sender, EventArgs e)
        {
            Connect();
            txtTenDangNhap.Text = "Admin";
            this.AcceptButton = buttonDangNhap;
            
        }
        
    }
}